package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.*;
import com.ysd.lis.entity.eq.EqSpotcheckPlan;
import com.ysd.lis.mapper.bac.BacAntibioticsDictMapper;
import com.ysd.lis.mapper.bac.BacGermInstrMapper;
import com.ysd.lis.mapper.bac.BacInstrAntibDictMapper;
import com.ysd.lis.mapper.bac.BacInstrGermDictMapper;
import com.ysd.lis.request.Params;
import com.ysd.lis.service.bac.BacGermInstrService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 细菌仪器表 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-11-25
 */
@Service
public class BacGermInstrServiceImpl extends ServiceImpl<BacGermInstrMapper, BacGermInstr> implements BacGermInstrService {
    @Autowired
    private BacInstrAntibDictMapper bacInstrAntibDictMapper;

    @Autowired
    private BacInstrGermDictMapper bacInstrGermDictMapper;
    @Override
    public Result getBacGermInstr(Params params) {
        LambdaQueryWrapper<BacGermInstr> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        //参数
        if (!StringUtils.isEmpty(params.getSearchValue())) {
            lambdaQueryWrapper.and(wrapper ->
                    wrapper.like(BacGermInstr::getInstrCode, params.getSearchValue())
                            .or().like(BacGermInstr::getInstrName, params.getSearchValue()));

        }
        lambdaQueryWrapper.eq(BacGermInstr::getDelFlag, 0);
        List<BacGermInstr> bacGermInstrs = this.baseMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "获取数据成功", bacGermInstrs);
    }

    @Override
    public Result saveOrUpdateInstr(List<BacGermInstr> instrList) {
        boolean b = this.saveOrUpdateBatch(instrList);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result deleteInstr(List<BacGermInstr> instrList) {
        List<String> ids = instrList.stream().map(BacGermInstr::getId).collect(Collectors.toList());
        List<String> instrCode = instrList.stream().map(BacGermInstr::getInstrCode).collect(Collectors.toList());
        this.baseMapper.deleteBatchIds(ids);
        bacInstrAntibDictMapper.delete(new LambdaQueryWrapper<BacInstrAntibDict>().in(BacInstrAntibDict::getInstrCode, instrCode).eq(BacInstrAntibDict::getDelFlag, 0));
        bacInstrGermDictMapper.delete(new LambdaQueryWrapper<BacInstrGermDict>().in(BacInstrGermDict::getInstrCode, instrCode).eq(BacInstrGermDict::getDelFlag, 0));
        return Result.succ(1, "成功", null);
    }
}
